package com.healthcloud.utils;

import com.google.common.base.Joiner;
import com.healthcloud.entity.HlHospitalEntity;
import com.healthcloud.entity.SysOrganiEntity;
import com.healthcloud.entity.SysUserEntity;
import com.healthcloud.service.HlHospitalService;

import com.healthcloud.service.hospital.SysOrganiService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 *
 * 共同方法
 * Created by yu on 2017/6/5.
 */
@Component
public class CommonMethod {

    @Autowired
    private HlHospitalService hlHospitalService;
    @Autowired
    private SysOrganiService sysOrganiService;


    /**
     * 返回为null时，表示超级管理员权限
     * @return
     */
    public  HlHospitalEntity getHospitalBean(){
        //所属医院
    	SysUserEntity user = ShiroUtils.getUserEntity();

        if(Constant.ADMIN.equals(user.getUsername())){
            return  null;
        }
        List<HlHospitalEntity> list = hlHospitalService.getHospitalsByUserId(user.getId());
        if (list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }
    
    public  Long getHospitalId(){
        HlHospitalEntity hospital = getHospitalBean();
		if(hospital != null){
			return hospital.getId();
		}
    	return null;
    }

    public static HttpSession getSession(){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession();
    }

    /**
     * 判断是否有最高权限
     * @return
     */
    public static boolean hasSuperRole(){
        return ShiroUtils.getSubject().isPermitted("super:role");
    }


    /**
     * 身份证转性别
     * @return
     */
    public static Integer idCardToGender(String idCard){
        try{
            Integer gender = Integer.parseInt(idCard.substring(16, 17));
            if(gender%2==0){
                gender = 2;
            }else{
                gender =1;
            }
            return  gender;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
